/**
 * 文件名：WidgetController.java
 *
 * 版本信息：v1.0
 * 日期：2013-6-22
 * Copyright  Corporation 2013 
 * 版权所有
 *
 */

package com.foreveross.bsl.mam.model.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.bsl.mam.model.application.WidgetSnapshotApplication;
import com.foreveross.bsl.mam.model.application.vo.WidgetSnapshotVO;

/**
 * 此类描述的是：快照业务逻辑控制器
 * 
 * @author: panhm@foreveross.com
 * @version: 2013-6-22 下午4:49:42
 */

@Controller
@RequestMapping(value = "/mam/widgetSnapshot")
public class WidgetSnapshotController extends WidgetBaseController {
	private Log log = LogFactory.getLog("WidgetSnapshotController");
	@Inject
	private WidgetSnapshotApplication widgetSnapshotApplication;

	/**
	 * 此方法描述的是：添加页面
	 * 
	 * @author: panhm@foreveross.com
	 * @version: 2013-6-22 下午4:55:28
	 */
	@RequestMapping("/add-view/{versionId}")
	public String addView(@PathVariable("versionId")String versionId, Model model) {

		model.addAttribute("versionId", versionId);
		return "model/snapshot-add";
	}

	/**
	 * 此方法描述的是：检查模块标识是否存在
	 * 
	 * @author: panhm@foreveross.com
	 * @version: 2013-6-22 下午4:55:28
	 */
	@ResponseBody
	@RequestMapping("/add")
	public Map<String, String> add(WidgetSnapshotVO snapshot) {

		Map<String, String> result = new HashMap<String, String>();
		try {
			Date date = new Date();
			snapshot.setCreated_at(date);
			snapshot.setModified_at(date);
			snapshot.setAvailable(1);
			widgetSnapshotApplication.saveEntity(snapshot);
			result.put("result", "success");
		} catch (Exception e) {
			log.error("[add]", e);
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, String> delete(String id) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			widgetSnapshotApplication.deleteEntity(id);
			result.put("result", "success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("[delete]", e);
			result.put("result", "error");
		}
		return result;
	}

}
